package com.yuxi.Service.Impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yuxi.Config.ConfigStatic;
import com.yuxi.Dao.InformationDao;
import com.yuxi.Dao.PowerRoleDao;
import com.yuxi.Dao.PowerUserDao;
import com.yuxi.Dao.PrisonRoleDao;
import com.yuxi.Dao.PrisonStructureDao;
import com.yuxi.Entity.Information;
import com.yuxi.Entity.InformationPort;
import com.yuxi.Entity.PowerUser;
import com.yuxi.Entity.PrisonRole;
import com.yuxi.Entity.PrisonStructure;
import com.yuxi.Entity.TaskInformationView;
import com.yuxi.Entity.UserInfo;
import com.yuxi.Request.InformationQuery;
import com.yuxi.Request.Page;
import com.yuxi.Response.InfoCount;
import com.yuxi.Service.UserQueryService;
import com.yuxi.Service.UserService;
import com.yuxi.view.UserPowerView;
@Service
public class UserQueryServiceImpl implements UserQueryService {
	@Autowired
	private InformationDao  informationDao;
	@Autowired
	private PowerRoleDao powerRoleDao;
	@Autowired
	private PowerUserDao powerUserDao;
	@Autowired
	private PrisonRoleDao prisonRoleDao;
	@Autowired
	private PrisonStructureDao prisonStructureDao;
	@Autowired
	private UserService userService;
	private Logger logger=LoggerFactory.getLogger(UserQueryServiceImpl.class);
	@Override
	public Page<Information> getInformationFirstCheck(InformationQuery inforQuery, UserInfo user, String ip,
			Page<Information> p,UserPowerView userPower) {
		try {
			inforQuery.setCurFLow(Integer.toString(ConfigStatic.FLOWTABLE.get("FirstCheck")));		
			String pId=Integer.toString(userPower.getMaxStructure().getId());
			List<InformationPort> userPortPower=userPower.getPowerPort();
			StringBuilder portStr=new StringBuilder("(");
			for(int i=0;i<userPortPower.size();i++){
				if(i>0){portStr.append(",");}			
				portStr.append(userPortPower.get(i).getId());
			}
			System.out.println(portStr);
			portStr.append(")");
			Page<Information> rsP=informationDao.getInfomationByQuery(inforQuery, p, "and prisonId = ? and safetyPortId in "+portStr.toString(),pId);
//			rsP.
			return rsP;
		} catch (Exception e) {
			e.printStackTrace();
			Page<Information> rsP=new Page<Information>();
			return rsP;
		}
		
		
	}

	@Override
	public Page<Information> getInformationReCheck(InformationQuery inforQuery, UserInfo user, String ip,
			Page<Information> p,Integer portId) {
		inforQuery.setCurFLow(Integer.toString(ConfigStatic.FLOWTABLE.get("ReCheck")));		
		Page<Information> rsP=informationDao.getInfomationByQuery(inforQuery, p, " and prisonId = ?",Integer.toString(portId));
		return rsP;		
	}

	@Override
	public Page<Information> getInformationCheck(InformationQuery inforQuery, UserInfo user, String ip,
			Page<Information> p,Integer portId) {		
		inforQuery.setCurFLow(Integer.toString(ConfigStatic.FLOWTABLE.get("Check")));		
		Page<Information> rsP=informationDao.getInfomationByQuery(inforQuery, p, " and prisonId = ?",Integer.toString(portId));
		return rsP;
	}

	@Override
	public Page<Information> getInformationInfoResearch(InformationQuery inforQuery, UserInfo user, String ip,
			Page<Information> p,UserPowerView userPower) { 	
		inforQuery.setCurFLow(Integer.toString(ConfigStatic.FLOWTABLE.get("InfoResearch")));	
		List<InformationPort> userPortPower=userPower.getPowerPort();
		String pId=Integer.toString(userPower.getMaxStructure().getId());
		StringBuilder portStr=new StringBuilder("(");
		for(int i=0;i<userPortPower.size();i++){
			if(i>0){portStr.append(",");}			
			portStr.append(userPortPower.get(i).getId());
		}
		portStr.append(")");
		Page<Information> rsP=informationDao.getInfomationByQuery(inforQuery, p, " and prisonId = ? and safetyPortId in "+portStr.toString(),pId);
		return rsP;
	}

	@Override
	public Page<Information> getInformationTaskDispatch(InformationQuery inforQuery, UserInfo user, String ip,
			Page<Information> p,Integer portId) {
		inforQuery.setCurFLow(Integer.toString(ConfigStatic.FLOWTABLE.get("TaskDispatch")));		
		Page<Information> rsP=informationDao.getInfomationByQuery(inforQuery, p, " and prisonId = ?",Integer.toString(portId));
		return rsP;
	}

	@Override
	public Page<Information> getInformationCurTaskDispatch(InformationQuery inforQuery, UserInfo user, String ip,
			Page<Information> p,Integer portId) {
		inforQuery.setCurFLow(Integer.toString(ConfigStatic.FLOWTABLE.get("CurTaskDispatch")));		
		Page<Information> rsP=informationDao.getInfomationByQuery(inforQuery, p, " and prisonId = ?",Integer.toString(portId));
		return rsP;
	}

	@Override
	public Page<TaskInformationView> getInformationInfoFinsh(InformationQuery inforQuery, UserInfo user, String ip,
			Page<TaskInformationView> p) {
		inforQuery.setCurFLow(Integer.toString(ConfigStatic.FLOWTABLE.get("TaskFinish")));		
		Page<TaskInformationView> rsP=informationDao.getUserZeRenInfoList(inforQuery, p, user.getUserId(),0);
		return rsP;
	}
	@Override
	public Page<TaskInformationView> getAllTimeOutInfoList(InformationQuery inforQuery, UserInfo user, String ip,
			Page<TaskInformationView> p) {
		inforQuery.setCurFLow(Integer.toString(ConfigStatic.FLOWTABLE.get("TaskFinish")));		
		Page<TaskInformationView> rsP=informationDao.getAllTimeOutInfoList(inforQuery, p, user.getUserId(),0);
		return rsP;
	}
	@Override
	public Page<TaskInformationView> getInformationAlreadyFinsh(InformationQuery infoQuery, UserInfo user, String ip,
			Page<TaskInformationView> p) {
		infoQuery.setCurFLow(Integer.toString(ConfigStatic.FLOWTABLE.get("InfoFinish")));		
		Page<TaskInformationView> rsP=informationDao.getUserZeRenInfoList(infoQuery, p, user.getUserId(),1);
		return rsP;
	}

	@Override
	public Page<Information> getInformationComment(InformationQuery infoQuery, UserInfo user, String ip,
			Page<Information> p,Integer portId) {
		infoQuery.setCurFLow(Integer.toString(ConfigStatic.FLOWTABLE.get("InfoComment")));		
		Page<Information> rsP=informationDao.getCommentInfomationByQuery(infoQuery, p, " and inforTable.prisonId = ? ",Integer.toString(portId));
		return rsP;
	}
	
	@Override
	public Page<Information> getInformationRate(InformationQuery infoQuery, UserInfo user, String ip,
			Page<Information> p) {
		infoQuery.setCurFLow(Integer.toString(ConfigStatic.FLOWTABLE.get("InfoEvaluation")));
		infoQuery.setInformationJudge("rate");
		Page<Information> rsP=informationDao.getRateInfomationByQuery(infoQuery, p,user.getUserId());
		return rsP;
	}

	@Override
	public Page<Information> getInformationAlreadyDoByFLow(InformationQuery infoQuery, int flowId, UserInfo user,
			String ip, Page<Information> p,List<InformationPort> ports) {	
		Page<Information> rsP=informationDao.getInformationAlreadyDoByFLow(infoQuery, flowId, p, user.getUserId(),ports);
		return rsP;
	}
	@Override
	public Page<Information> getAllInformation(InformationQuery infoQuery , Page<Information> p,PrisonStructure pStructure) {	
		
		Page<Information> rsP=informationDao.getAllInformation(infoQuery, p,pStructure);
		return rsP;
	}
	@Override
	public Page<Information> getLookCommentInformation(InformationQuery infoQuery , Page<Information> p,UserInfo u) {	
		
		Page<Information> rsP=informationDao.getAllCommentInformation(infoQuery, p,u.getUserId());
		return rsP;
	}
	@Override
	public Page<Information> getLookEvaluationInformation(InformationQuery infoQuery , Page<Information> p,UserInfo u) {	
		
		Page<Information> rsP=informationDao.getAllEvaluationInformation(infoQuery, p,u.getUserId());
		return rsP;
	}
	@Override
	public Page<TaskInformationView> getAreadyInputTaskContext(InformationQuery inforQuery, UserInfo user, String ip,
			Page<TaskInformationView> p) {
		inforQuery.setCurFLow(Integer.toString(ConfigStatic.FLOWTABLE.get("TaskFinish")));		
		Page<TaskInformationView> rsP=informationDao.getUserZeRenInfoList(inforQuery, p, user.getUserId(),1);
		return rsP;
	}

	@Override
	public Page<Information> getAllinfoList(InformationQuery inforQuery, UserInfo user, Page<Information> p ) {
		try{
			PrisonStructure pu=userService.getUserPrisonArea(user.getUserId());
			Page<Information> rsp=informationDao.getAllInfoList(inforQuery, p,Integer.toString(pu.getId()));
			if(rsp==null){
				logger.error(user.getUserAccount()+": 获取所有信息列表失败");
				return null;
			}
			logger.info(user.getUserAccount()+": 获取所有信息列表成功");
			return rsp;
		}catch(Exception e){
			logger.error(user.getUserAccount()+": 获取所有信息列表失败");
			return null;
		}
	}

	@Override
	public InfoCount getFlowCount(UserInfo u,UserPowerView power) {		
		return  informationDao.getAllFlowCount(u, power);
	}

	@Override
	public Page<Information> getInfoDispatch(InformationQuery inforQuery, Page<Information> p, UserInfo u ) {
		return informationDao.getAreadyDisInfo(inforQuery, p, u);
	}

	@Override
	public Page<TaskInformationView> getUserDispatchList(InformationQuery inforQuery, Page p, UserInfo u ) {		
		return informationDao.getUserDispatchList(inforQuery, p, u.getUserId());
	}

	@Override
	public Page<Information> getCanTaskDisList(InformationQuery inforQuery, Page<Information> p, UserInfo u ) {
		PowerUser pu=powerUserDao.getPowerUserByUserId(u.getUserId());
		PrisonRole role=prisonRoleDao.getPrisonRole(pu.getRoleId());
		int portId=role.getPrisonStructure().getId();
		return informationDao.getCanTaskDisList(inforQuery, p, Integer.toString(portId));
	}
	
	@Override
	public Page<Information> getAllinfoList(InformationQuery inforQuery, UserInfo user, Page<Information> p,Integer portID ) {
		try{		
			Page<Information> rsp=informationDao.getAllInfoList(inforQuery, p,Integer.toString(portID));
			if(rsp==null){
				logger.error(user.getUserAccount()+": 获取所有信息列表失败");
				return null;
			}
			logger.info(user.getUserAccount()+": 获取所有信息列表成功");
			return rsp;
		}catch(Exception e){
			logger.error(user.getUserAccount()+": 获取所有信息列表失败");
			return null;
		}
	}

	
	

}
